//
//  Factory.m
//  HeroNuts
//
//  Created by Huo Ju on 11-4-13.
//  Copyright 2011年 TongJi. All rights reserved.
//

#import "HNActorFactory.h"
#import "HeroNuts.h"

@interface HNActorFactory ()
-(id)initActor:(HNActor *)actor  fromConfig:(NSString *)classname;
@end

@implementation HNActorFactory
@synthesize config = config_;

static HNActorFactory * factory_  = nil;

-(id)init
{
    if((self = [super init]))
    {
        config_ = NSClassFromString(@"Config");
    }
    return self;
}

+ (HNActorFactory*)sharedActorFactory
{
	if (!factory_)
    {
        factory_ = [[HNActorFactory alloc] init];
    }
    
	return factory_;
}

-(HNActor *)newActorWithClassName:(NSString *)classname
{
    Class obj = NSClassFromString(classname);
    HNActor *actor = [[obj alloc] init];
    
    [self initActor:actor fromConfig:classname];
    return actor;
}

-(id)initActor:(HNActor *)actor  fromConfig:(NSString *)classname
{
    NSDictionary *instanceInfo = [[config_ class] dictForObjectId:classname];
    id class = [instanceInfo objectForKey:@"class"];
    [actor setValue:class forKey:@"class"];
    
    
    NSDictionary* config = [instanceInfo objectForKey:@"config"];
    for (NSString* key in config) {
        [actor setValue:[config objectForKey:key] forKey:key];
         NSLog(@"%@,%@",key,[config objectForKey:key]);
    }
   
    return actor;
}
@end
